package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.UpDateTaskBean;
import com.wnhz.work.yz.bean.TaskPersonBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddTask2Activity extends BaseActivity implements View.OnClickListener {

    private EditText task_name, task_msg;
    private TextView task_plan;
    private TextView task_start;
    private TextView task_end;
    private TextView task_boss;
    private TextView task_person;
    private int max_cololr;
    private static final int GETPLANID = 12344;
    private static final int GETMANAGER = 12345;
    private static final int GETPERSON = 12346;
    private RecyclerView manager, person;
    private BaseRVAdapter managerAdapter, personAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_task);
        initData();
        initTitle();
        bindViews();
        initListener();
        if (type != -1) {
            upLoad();
        }
    }

    private int type;
    private String task_id;

    private void initData() {
        type = getIntent().getIntExtra("type", -1);
        task_id = getIntent().getStringExtra("task_id");
    }

    private UpDateTaskBean.InfoEntity update = new UpDateTaskBean.InfoEntity();

    private void setUpdateInfo() {
        List<UpDateTaskBean.InfoEntity.MainUserIdEntity> main_user_id = update.getMain_user_id();
        for (int i = 0; i < main_user_id.size(); i++) {
            TaskPersonBean tpb = new TaskPersonBean();
            tpb.setUser_name(main_user_id.get(i).getRealname());
            tpb.setUser_img(main_user_id.get(i).getUser_img());
            tpb.setUser_id(main_user_id.get(i).getUser_id());
            taskBeen.add(tpb);
        }

        List<UpDateTaskBean.InfoEntity.PartUserIdEntity> part_user_id = update.getPart_user_id();
        for (int i = 0; i < part_user_id.size(); i++) {
            TaskPersonBean tpb = new TaskPersonBean();
            tpb.setUser_name(part_user_id.get(i).getRealname());
            tpb.setUser_img(part_user_id.get(i).getUser_img());
            tpb.setUser_id(part_user_id.get(i).getUser_id());
            taskBeen2.add(tpb);

        }
        task_name.setText(update.getTitle());
        task_name.setEnabled(false);
        task_msg.setText(update.getContent());
        task_msg.setEnabled(false);
        task_plan.setText(update.getPlanning_name());
        task_start.setText(update.getStart_time());
        task_end.setText(update.getEnd_time());
//        managerAdapter.notifyDataSetChanged();
//        personAdapter.notifyDataSetChanged();
        Log.e("===", "" + taskBeen.size() + "---" + taskBeen2.size());
        setManagerData();
        setData();
    }


    private void upLoad() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("task_id", task_id);
        showDialog();
        XUtil.Post(Url.TASK_EDITTASK, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        UpDateTaskBean bean = gson.fromJson(result, UpDateTaskBean.class);
                        update = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setUpdateInfo();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    private void setManagerData() {
        managerAdapter = new BaseRVAdapter(this, taskBeen) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_add_person2;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.img_head), taskBeen.get(position).getUser_img());
                holder.setTextView(R.id.text_name, taskBeen.get(position).getUser_name());
                holder.getView(R.id.delete).setVisibility(View.GONE);
            }
        };
        manager.setAdapter(managerAdapter);
    }

    private void setData() {

        personAdapter = new BaseRVAdapter(this, taskBeen2) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_add_person2;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.img_head), taskBeen2.get(position).getUser_img());
                holder.setTextView(R.id.text_name, taskBeen2.get(position).getUser_name());
                holder.getView(R.id.delete).setVisibility(View.GONE);
            }
        };
        person.setAdapter(personAdapter);
    }


    private void bindViews() {
        manager = (RecyclerView) findViewById(R.id.manager);
        manager.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        person = (RecyclerView) findViewById(R.id.person);
        person.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        //
        max_cololr = getResources().getColor(R.color.main_color);
        task_name = (EditText) findViewById(R.id.task_name);
        task_plan = (TextView) findViewById(R.id.task_plan);
        task_start = (TextView) findViewById(R.id.task_start);
        task_end = (TextView) findViewById(R.id.task_end);
        task_boss = (TextView) findViewById(R.id.task_boss);
        task_person = (TextView) findViewById(R.id.task_person);
        task_msg = (EditText) findViewById(R.id.task_msg);
    }

    private void initListener() {
        task_plan.setOnClickListener(this);
        task_start.setOnClickListener(this);
        task_end.setOnClickListener(this);
        task_boss.setOnClickListener(this);
        task_person.setOnClickListener(this);
    }


    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);

        title.setTextColor(getResources().getColor(R.color.main_color));
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        title.setText("任务详情");
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
        }
    }


    private List<TaskPersonBean> taskBeen = new ArrayList<>();
    private List<TaskPersonBean> taskBeen2 = new ArrayList<>();


}
